﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Northwind;
using System.Linq.Expressions;
using System.Data.Common;
using System.Reflection;

namespace Test
{	
	[TestClass]
	public class UnitTest1 : UnitTestClassBase
	{		
		

		[TestMethod]		
		public void ConstantQuery() {
			DbQueryProvider provider = new DbQueryProvider(this.connection);
			Query<Customer> query = new Query<Customer>(provider);
			WriteQueryText(query.Expression);
			TestContext.WriteLine("count: {0}", query.Count());
			foreach (var c in query) {
				TestContext.WriteLine("{0}, {1}", c.ID, c.Code);
			}
		}

		[TestMethod]
		[TestCategory("Where")]
		public void WhereNested() {
			const int MAX = 100;
			const int MIN = 10;
			DbQueryProvider provider = new DbQueryProvider(this.connection);
			var query = new Query<Customer>(provider)
				.Where(c => c.ID > MIN || c.ID < MAX)
				.Where(c=> c.Code != "ALFKI" || c.Code == "ANTON");
			foreach (var c in query) {
				TestContext.WriteLine("{0}, {1}", c.ID, c.Code);
			}
			WriteQueryText(query.Expression);
		}

		[TestMethod]
		[TestCategory("Join")]
		public void Join1() {
			const int MAX = 100;
			const int MIN = 10;
			DateTime orderDate = new DateTime(1997, 01, 01);
			DbQueryProvider provider = new DbQueryProvider(this.connection);
			var query = new Query<Customer>(provider)
				.Where(c => c.ID > MIN || c.ID < MAX)				
				.Join( (new Query<Order>(provider).Where(o => o.ID < 10420)), 
				c => c.ID, 
				o => o.CustomerId, (c,o) => c).Take(100);			
			WriteQueryText(query.Expression);
			foreach (var c in query) {
				TestContext.WriteLine("{0}, {1}", c.ID, c.Code);
			}
		}

		[TestMethod]
		[TestCategory("Count")]
		public void CountTest() {
			DateTime orderDate = new DateTime(1997, 01, 01);
			DbQueryProvider provider = new DbQueryProvider(this.connection);
			var query = new Query<Customer>(provider)			
				.Join((new Query<Order>(provider).Where(o => o.ID < 10420)),
				c => c.ID,
				o => o.CustomerId, (c, o) => c).Take(1000);
			TestContext.WriteLine("count: {0}", query.Count());
			WriteQueryText(query.Expression);						
		}

		[TestMethod]
		[TestCategory("Join")]
		public void ManyJoins() {
			DateTime orderDate = new DateTime(1997, 01, 01);
			DbQueryProvider provider = new DbQueryProvider(this.connection);
			var query = new Query<Customer>(provider)
				.Join((new Query<Order>(provider).Where(o => o.ID < 10420)),
				c => c.ID,
				o => o.CustomerId, (c, o) => o)
				.Join(new Query<Shipper>(provider), o => o.ShipperId, s => s.ID, (o,s) => s);
			TestContext.WriteLine("count: {0}", query.Count());
			WriteQueryText(query.Expression);
		}

	
		[TestMethod]
		[TestCategory("SelectMany")]
		public void SelectManyTest() {
			DateTime orderDate = new DateTime(1997, 01, 01);
			DbQueryProvider provider = new DbQueryProvider(this.connection);
			var query = new Query<Customer>(provider)
				.Join((new Query<Order>(provider).Where(o => o.ID < 10420)),
				c => c.ID,
				o => o.CustomerId, (c, o) => o)
				.SelectMany(o => new Query<Shipper>(provider).Where(s => o.ShipperId == s.ID));
			TestContext.WriteLine("count: {0}", query.Count());
			WriteQueryText(query.Expression);
		}

	
	}
}
